package com.qf.mao.service;

import com.qf.mao.dao.ClazzDao;
import com.qf.mao.entity.Clazz;
import com.qf.mao.entity.ClazzDTO;
import com.qf.mao.entity.RespPageBean;
import com.qf.mao.utils.CommonsUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * 1 10
 * 2 10
 * 3 10
 *
 * select * from clazz limit 0,10
 * select * from clazz limit 10,10
 * select * from clazz limit 20,10
 */
@SuppressWarnings("all")
public class ClazzService {
    ClazzDao clazzDao = new ClazzDao();
    public RespPageBean getClazzByPage(String gid, String page, String rows, String sort, String order) {
        try {
            //在当前查询条件下，总记录数
            Long total = clazzDao.getTotal(gid);
            int offset = Integer.parseInt(page);
            int size = Integer.parseInt(rows);
            //按照条件进行查询
            List<ClazzDTO> list = clazzDao.getClazzByPage(gid,(offset-1)*size,size,sort,order);
            return new RespPageBean(total,list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public Integer addClazz(String clazzName, int gid) {
        Integer r = null;
        try {
            Clazz c = clazzDao.getClazzByClazzName(clazzName);
            if (c != null) {
                return CommonsUtils.ERROR_NAME;
            }
            r = clazzDao.addClazz(clazzName, gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r == 1 ? CommonsUtils.SUCCESS_OK : CommonsUtils.ERROR_ELSE;
    }

    public Integer deleteClazzByGid(String cid) {
        Integer r = null;
        try {
            r = clazzDao.deleteClazzByCid(Integer.parseInt(cid));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r == 1 ? CommonsUtils.SUCCESS_DELETE : CommonsUtils.ERROR_ELSE;
    }
}
